<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">配货需求单号：</label>
                                    <div class="layui-input-inline">
                                        <input name="distCode" class="layui-input" type="text" placeholder="请输入配货需求单号"/>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="distributionDemandBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="distributionDemandTempTable" lay-filter="distributionDemandTempTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="distributionDemandTempTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
    {{# if(d.status=='created'){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="audit">审核</a>
    {{#} }}
    {{# if(d.status=='rejected'){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="submit">提交</a>
    {{#} }}
    <!--
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    -->
</script>

<script type="text/html" id="distributionDemandTempProductTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>



<!-- 表单弹窗 -->
<script type="text/html" id="distributionDemandTempForm">
    <form lay-filter="distributionDemandTempForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">配货需求单类型</label>
            <div class="layui-input-block">
                <select name="type">
<!--                    <option value="alarm">告警配货需求单</option>-->
                    <option value="temp">临时配货需求单</option>
                </select>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-col-xs4">
                <label class="layui-form-label">省</label>
                <div class="layui-input-block">
                    <select name="province" id="province" lay-filter="province" lay-verify="required" required></select>
                </div>
            </div>
            <div class="layui-col-xs4">
                <label class="layui-form-label">市</label>
                <div class="layui-input-block">
                    <select name="city" id="city" lay-filter="city" lay-verify="required" required></select>
                </div>
            </div>
            <div class="layui-col-xs4">
                <label class="layui-form-label">区/县</label>
                <div class="layui-input-block">
                    <select name="area" id="area" lay-filter="area" lay-verify="required" required></select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">详情地址</label>
            <div class="layui-input-block">
                <input name="address" placeholder="请输入详情地址" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">客户联系人</label>
            <div class="layui-input-block">
                <input name="customerConcat" placeholder="请输入客户联系人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" id="phoneDiv">
            <label class="layui-form-label">联系人电话</label>
            <div class="layui-input-block">
                <input name="customerMoble" placeholder="请输入联系人电话" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" id="productBtnAddDiv">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <button id="productBtnAdd" class="layui-btn icon-btn" onclick="return false;"><i class="layui-icon">&#xe654;</i>添加配货物资信息</button>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <table class="layui-table" id="distributionDemandTempProductTable" lay-filter="distributionDemandTempProductTable"></table>
            </div>
        </div>

        <div id="tailDiv"></div>
        <!--
        <div class="layui-form-item text-right saveOrCancleBtn">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn saveBtn" lay-filter="distributionDemandTempFormSubmit" lay-submit>提交</button>
        </div>
        -->
    </form>
</script>

<script type="text/html" id="platformProductTab">

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">物料编号：</label>
                            <div class="layui-input-inline">
                                <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-filter="formSubSearchLog4Product" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                            <button id="distributionDemandProductBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>确认添加</button>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="platformProductTable" lay-filter="platformProductTable"></table>
            </div>
        </div>
    </div>

</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#distributionDemandTempTable',
            url: config.base + 'enterprisecab/order/admin/distributionDemandTemp/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers',title:'序号'}

                ,{field: 'distCode', sort: true, title: '配货需求单编号'}
                ,{field: 'enterpriseName', sort: true, title: '所属公司'}
                ,{field: 'cabSkuNum', sort: true, title: '补货数量'}
                ,{field: 'cabNum', sort: true, title: '补货柜子数量'}
                ,{field: 'total', sort: true, title: '货值'}

                // ,{field: 'province', sort: true, title: '省'}
                // ,{field: 'city', sort: true, title: '市'}
                // ,{field: 'area', sort: true, title: '区'}
                ,{field: 'address', sort: true, title: '详情地址'}
                ,{field: 'customerConcat', sort: true, title: '客户联系人'}
                ,{field: 'customerMoble', sort: true, title: '联系人电话'}
                ,{field: 'creatorName', sort: true, title: '创建人'}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{field: 'statusDesc', sort: true, title: '状态',templet:function (d) {
                        if(d.status=='pending'){
                            return '已审核';
                        }else if(d.status=='created'){
                            return '已提交';
                        }
                        return d.statusDesc;
                    }}


                // ,{field: 'addNum', sort: true, title: '刀具补货数量'}
                // ,{field: 'stockNum', sort: true, title: '刀柜物料库存数量'}
                // ,{field: 'enterpriseId', sort: true, title: '企业id'}
                // ,{field: 'confirmTime', sort: true, title: '确认时间'}
                // ,{field: 'statusDesc', sort: true, title: '状态'}
                // ,{field: 'unitId', sort: true, title: '商品单位id'}
                // ,{field: 'unitName', sort: true, title: '商品单位名称'}
                // ,{field: 'workshopId', sort: true, title: '车间id'}
                // ,{field: 'workshopName', sort: true, title: '车间名称'}
                // ,{field: 'type', sort: true, title: '配货需求单类型：alarm(0,告警配货需求单);temp(1临时配货需求单)'}

                ,{align: 'center', toolbar: '#distributionDemandTempTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton&&JSON.stringify(menuButton)!='{}'){
                    if(menuButton.indexOf("搜索") == -1){
                        $('.layui-form.toolbar').remove();
                    }
                    if(menuButton.indexOf("修改") == -1){
                        $("[lay-event='edit']").remove();
                    }
                    if(menuButton.indexOf("删除") == -1){
                        $("[lay-event='del']").remove();
                    }
                }

            }
        });

        // 工具条点击事件
        table.on('tool(distributionDemandTempTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                data.eventType='edit';
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }else if(obj.event=='detail'){
                data.eventType='detail';
                showEditModel(data);
            }else if(obj.event=='audit'){
                data.eventType='audit';
                showEditModel(data);
            }else if(obj.event=='submit'){
                data.eventType='submit';
                showEditModel(data);
            }
        });

        //监听排序
        table.on('sort(distributionDemandTempTable)', function(obj){
            table.reload('distributionDemandTempTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 添加按钮点击事件
        $('#distributionDemandBtnAdd').click(function () {
            showEditModel();
        });





        // 显示编辑弹窗
        function showEditModel(data) {
            var productSelectData=[];
            var productSelectMap=new Map();
            var title='';
            if(data==undefined){
                title='添加临时配货需求订单表';
            }else if(data.eventType=='detail'){
                title='详情';
            }else if(data.eventType=='audit'){
                title='审核';
            }else if(data.eventType=='submit'){
                title='提交';
            }
            admin.open({
                type: 1,
                area: '1580px',
                offset: '65px',
                title: title,
                content: $('#distributionDemandTempForm').html(),
                success: function () {

                    var btnHtml=''
                    if(data&&data.eventType=='audit'){

                        form.render();
                        btnHtml+='<div class="layui-form-item auditDiv">' +
                            '            <label class="layui-form-label">审核/驳回意见</label>' +
                            '            <div class="layui-input-block">' +
                            '                <textarea id="auditRemark" name="auditRemark" placeholder="请输入审核/驳回意见" class="layui-textarea" />' +
                            '            </div>' +
                            '        </div>';
                        btnHtml+='<div class="layui-form-item text-right saveBtn">'+
                            '<button  class="layui-btn layui-btn-primary"  lay-filter="distributionDemandFormReject" lay-submit>驳回</button>'+
                            '<button  class="layui-btn "  lay-filter="distributionDemandFormApproved" lay-submit>审核通过</button>' +
                            '        </div>';
                        $("#tailDiv").before(btnHtml);
                    }else if(data&&data.eventType=='detail'){
                        btnHtml+='<div class="layui-form-item">' +
                            '            <label class="layui-form-label">提交时间</label>' +
                            '            <div class="layui-input-block">' +
                            '                <input name="createTime" placeholder="请输入创建时间" type="text" class="layui-input" lay-verify="" required/>' +
                            '            </div>' +
                            '        </div>'+
                            '<div class="layui-form-item">' +
                            '            <label class="layui-form-label">提交人</label>' +
                            '            <div class="layui-input-block">' +
                            '                <input name="creatorName" placeholder="" type="text" class="layui-input" lay-verify="" required/>' +
                            '            </div>' +
                            '        </div>'+
                            // '<div class="layui-form-item">' +
                            // '            <label class="layui-form-label">提交时间</label>' +
                            // '            <div class="layui-input-block">' +
                            // '                <input name="submitTime" placeholder="请输入提交时间" type="text" class="layui-input" lay-verify="" required/>' +
                            // '            </div>' +
                            // '        </div>'+
                            // '<div class="layui-form-item">' +
                            // '            <label class="layui-form-label">提交人</label>' +
                            // '            <div class="layui-input-block">' +
                            // '                <input name="submitterName" placeholder="" type="text" class="layui-input" lay-verify="" required/>' +
                            // '            </div>' +
                            // '        </div>'+

                            '<div class="layui-form-item">' +
                            '            <label class="layui-form-label">审核时间</label>' +
                            '            <div class="layui-input-block">' +
                            '                <input name="auditTime" placeholder="请输入审核时间" type="text" class="layui-input" lay-verify="" required/>' +
                            '            </div>' +
                            '        </div>'+
                            '<div class="layui-form-item">' +
                            '            <label class="layui-form-label">审核人</label>' +
                            '            <div class="layui-input-block">' +
                            '                <input name="auditorName" placeholder="" type="text" class="layui-input" lay-verify="" required/>' +
                            '            </div>' +
                            '        </div>'+
                            '<div class="layui-form-item">' +
                            '            <label class="layui-form-label">审核/驳回意见</label>' +
                            '            <div class="layui-input-block">' +
                            '                <textarea id="auditRemark" name="auditRemark" placeholder="请输入审核/驳回意见" class="layui-textarea" />' +
                            '            </div>' +
                            '        </div>';
                        $("#phoneDiv").after(btnHtml);
                    }else {
                        btnHtml='<div class="layui-form-item text-right saveBtn">' +
                            '            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>' +
                            '            <button class="layui-btn " lay-filter="distributionDemandTempFormSubmit" lay-submit>提交</button>' +
                            '        </div>';
                        $("#tailDiv").before(btnHtml);
                    }


                    form.render();

                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }
                    productSelectData=[];
                    productSelectMap=new Map();

                    var selectArea=function(city){
                        admin.req('enterprisecab/area/admin/area/getArea?parentId=' + city,{}, function(res) {
                            layer.closeAll('loading');
                            if (200 == res.code) {
                                $('#area').append(new Option("请选择区", ""));//往下拉菜单里添加元素
                                $.each(res.data, function(index, item) {
                                    $('#area').append(new Option(item.name, item.id));//往下拉菜单里添加元素
                                })
                                form.val('distributionDemandTempForm', data);
                                form.render(); //菜单渲染 把内容加载进去
                            } else {
                                layer.msg('获取(区县)区域信息失败!', {
                                    icon : 2,
                                    time : 1000
                                });
                            }
                        }, 'GET');
                    }

                    var selectCity=function(province){
                        admin.req('enterprisecab/area/admin/area/getCity?parentId=' + province,{}, function(res) {
                            layer.closeAll('loading');
                            if (200 == res.code) {
                                $('#city').append(new Option("请选择市", ""));//往下拉菜单里添加元素
                                $.each(res.data, function(index, item) {
                                    $('#city').append(new Option(item.name, item.id));//往下拉菜单里添加元素
                                })
                                form.val('distributionDemandTempForm', data);
                                form.render(); //菜单渲染 把内容加载进去
                                if(data){
                                    selectArea(data.city);
                                }
                            } else {
                                layer.msg('获取(市)区域信息失败!', {
                                    icon : 2,
                                    time : 1000
                                });
                            }
                        }, 'GET');
                    }
                    // 获取省区域信息
                    admin.req('enterprisecab/area/admin/area/getProvince', {}, function(res) {
                        layer.closeAll('loading');
                        if (200 == res.code) {
                            $('#province').append(new Option("请选择省", ""));//往下拉菜单里添加元素
                            $.each(res.data, function(index, item) {
                                $('#province').append(new Option(item.name, item.id));//往下拉菜单里添加元素
                            })

                            form.val('distributionDemandTempForm', data);
                            form.render(); //菜单渲染 把内容加载进去
                            if(data){
                                selectCity(data.province);
                            }
                        } else {
                            layer.msg('获取(省)区域信息失败!', {
                                icon : 2,
                                time : 1000
                            });
                        }
                    }, 'GET', true);

                    form.on('select(province)', function(data) {
                        $("#city").empty();
                        $('#area').empty();

                        // 获取市区域信息
                        if(data.value!=''){
                            selectCity(data.value);
                        }
                    });

                    form.on('select(city)', function(data) {
                        $('#area').empty();

                        // 获取区县区域信息
                        if(data.value!=''){
                            selectArea(data.value);
                        }
                    });

                    $("#productBtnAdd").click(function () {
                        showEditProductModel();
                    })

                    if(data){
                        admin.req('enterprisecab/productCab/admin/productDetail/list' ,{distributionDemandId:data.id}, function(res) {
                            layer.closeAll('loading');
                            if (200 == res.code) {
                                fillSelectProduct(res.data,data.eventType);
                            } else {
                                layer.msg('获取配货明细失败!', {
                                    icon : 2,
                                    time : 1000
                                });
                            }
                        }, 'GET',false);

                    }

                    if(data&&(data.eventType=='detail'||data.eventType=='audit')){
                        $("#productBtnAddDiv").hide();
                        if(data&&data.eventType!='audit'){
                            $(".saveBtn").hide();
                        }

                        // $(".model-form input").css("border","none")
                        $(".model-form input").attr('readonly',true);
                        $(".model-form select").attr('readonly',true);
                    }



                    form.val('distributionDemandTempForm', data);
                    // 表单提交事件
                    form.on('submit(distributionDemandTempFormSubmit)', function (d) {
                        // layer.load(2);
                        if(productSelectData.length==0){
                            layer.msg('请选择配货物资信息', {icon: 2});
                            return false;
                        }
                        var cabNum=0;
                        var skuNum=0;
                        productSelectData.forEach(item=>{
                            if(item.productType=='knife'){
                                skuNum++;
                            }else{
                                cabNum++;
                            }
                        })
                        d.field.cabNum=cabNum;
                        d.field.cabSkuNum=skuNum;


                        var setQuantityConfirm=function(){
                            var quantityConfirmMap=new Map();
                            $(".quantity").each(function(index,item){
                                var id=item.id;
                                var startIndex=id.indexOf("-");

                                var skuCode=id.substr(startIndex+1,id.length-startIndex-1);

                                var val=$(this).val();
                                quantityConfirmMap.set(skuCode,val);
                            })

                            productSelectData.forEach(item=>{
                                var newQuantity=quantityConfirmMap.get(item.skuCode);
                                if(newQuantity){
                                    item.quantityConfirm=parseInt(newQuantity);
                                }
                            })
                        }
                        setQuantityConfirm();
                        d.field.detailList=productSelectData;
                        admin.req(data ? 'enterprisecab/order/admin/distributionDemandTemp/update' : 'enterprisecab/order/admin/distributionDemandTemp/save', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('distributionDemandTempTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST',true,'application/json; charset=utf-8');
                        return false;
                    });


                    form.on('submit(distributionDemandFormApproved)', function (d) {
                        var id=data.id;
                        var remark=$("#auditRemark").val();
                        layer.load(2);

                        admin.req('enterprisecab/order/admin/distributionDemandTemp/audit/'+id, {status:'pending',auditRemark:remark}, function (res) {
                            layer.closeAll('loading');
                            if ( res.code==200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('distributionDemandTempTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');

                        return false;
                    });

                    form.on('submit(distributionDemandFormReject)', function (d) {
                        var id=data.id;
                        var remark=$("#auditRemark").val();
                        layer.load(2);
                        admin.req('enterprisecab/order/admin/distributionDemandTemp/audit/'+id, {status:'rejected',auditRemark:remark}, function (res) {
                            layer.closeAll('loading');
                            if ( res.code==200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('distributionDemandTempTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');

                        return false;
                    });

                    function addListener(tableId,paramData){
                        $('[lay-id='+ tableId + '] .layui-table-body .quantity').on('input',function (){
                            let value = $(this).val();
                            let attr = $(this).attr('id');
                            var startIndex=attr.indexOf('-');
                            var skuCode=attr.substr(startIndex+1,attr.length-startIndex-1);
                            // let idArr = attr.split('-');
                            productSelectData.forEach((item,index) => {
                                if(item.skuCode == skuCode){
                                    productSelectData[index].quantityConfirm = value;
                                    var price=productSelectData[index].price;
                                    var total=0;
                                    if(price){
                                        total=price*parseInt(value);
                                    }
                                    productSelectData[index].total=total;
                                    $("#quantityTotal-"+item.skuCode).html(total);
                                }
                            })

                        })
                    }

                    // var productSelectData=[];
                    // var productSelectMap=new Map();
                    function fillSelectProduct(data,eventType){
                        if(data&&data.length>0){
                            if (productSelectData.length==0){
                                productSelectData=data;
                                data.forEach(item=>{
                                    productSelectMap.set(item.skuCode,item);
                                })
                            }else {
                                data.forEach(item => {
                                    if(productSelectMap.get(item.skuCode)==undefined){
                                        productSelectData.push(item);
                                        productSelectMap.set(item.skuCode,item);
                                    }
                                })
                            }
                        }
                        var cols=[
                            {type: 'numbers',title:'序号'}
                            // ,{field: 'creatorName', sort: true, title: '创建人'}
                            // ,{field: 'modifierName', sort: true, title: '修改人'}
                            // ,{field: 'productId', sort: true, title: '商品ID'}
                            ,{field: 'skuCode', sort: true, title: '物料编号'}
                            ,{field: 'skuName', sort: true, title: '物料名称'}
                            ,{field: 'skuSpecValueNames', sort: true, title: '商品规格码'}
                            ,{field: 'unitName', sort: true, title: '单位'}
                            ,{field: 'productBrandName', sort: true, title: '品牌'}
                            ,{field: 'quantityStock', sort: true, title: '当前库存'}
                            ,{field: 'marketPrice', sort: true, title: '单价'}
                            ,{field: 'quantityConfirm', sort: true, title: '配货数',templet:function (d) {
                                    if(eventType=='detail'||eventType=='audit'){
                                        return d.quantityConfirm;
                                    }
                                    if(d.quantityConfirm){
                                        return '<div><input id="quantityConfirm-'+d.skuCode+'" type="text" value="'+d.quantityConfirm+'" class="layui-input quantity" lay-verify="number"/></div>';
                                    }else{
                                        return '<div><input id="quantityConfirm-'+d.skuCode+'" type="text" value="" class="layui-input quantity" lay-verify="number" value=""/></div>';
                                    }

                                }}
                            ,{field: 'total', sort: true, title: '总价',templet:function (d) {
                                    if(d.total){
                                        return '<div id="quantityTotal-'+d.skuCode+'">'+d.total+'</div>';
                                    }else{
                                        return '<div id="quantityTotal-'+d.skuCode+'"></div>';
                                    }
                                }}

                            // ,{field: 'skuSpec', sort: true, title: '商品规格列表'}
                            // ,{field: 'skuSpecValue', sort: true, title: '商品规格值列表'}
                            //
                            // ,{field: 'enterpriceCode', sort: true, title: '企业货号'}
                            // ,{field: 'spPrice', sort: true, title: '渠道价'}
                            //
                            // ,{field: 'storePrice', sort: true, title: '商品店铺价'}
                            // ,{field: 'activityPrice', sort: true, title: '最低活动价'}
                            // ,{field: 'supplyPrice', sort: true, title: '平台供货价'}
                            // ,{field: 'stockNumber', sort: true, title: '库存数'}
                            // ,{field: 'barcode', sort: true, title: '商品条码'}
                            // ,{field: 'sortNumber', sort: true, title: '排序'}
                            // ,{field: 'settledCompanyId', sort: true, title: '入驻企业ID'}
                            // ,{field: 'isSyns', sort: true, title: '是否已同步：no(0,未同步);yes(1,已同步);'}
                            // ,{field: 'dataSourceTypeDesc', sort: true, title: '数据来源'}
                            // ,{field: 'idPlatform', sort: true, title: '平台端表id'}
                            // ,{field: 'synsTime', sort: true, title: '同步时间'}
                            // ,{field: 'skuCodeAs', sort: true, title: '用于区分云端云仓手动添加物料'}
                            // ,{field: 'createTime', sort: true, title: '创建时间'}
                            // ,{align: 'center', toolbar: '#distributionDemandTempProductTableBar', title: '操作', width: 200}
                        ]
                        if(eventType!='detail'&&eventType!='audit'){
                            var col={align: 'center', toolbar: '#distributionDemandTempProductTableBar', title: '操作', width: 200};
                            cols.push(col);
                        }
                        var insTb = table.render({
                            elem: '#distributionDemandTempProductTable',
                            data: productSelectData,
                            page: false,
                            cellMinWidth: 100,
                            cols: [cols],
                            done: function (res, curr, count) {
                                addListener('distributionDemandTempProductTable',res.data)
                            }
                        });

                        // 工具条点击事件
                        table.on('tool(distributionDemandTempProductTable)', function (obj) {
                            var data = obj.data;
                            if (obj.event === 'edit') { //修改
                            } else if (obj.event === 'del') { // 删除
                                doDelete(obj);
                            }
                        });




                        function doDelete(obj) {
                            layer.confirm('确定要删除吗？', {
                                offset: '65px',
                                skin: 'layui-layer-admin'
                            }, function (i) {
                                layer.close(i);
                                // layer.load(2);
                                if(obj.data.detailId){
                                    admin.req('enterprisecab/order/admin/distributionDemandDetail/' + obj.data.detailId, {}, function (res) {
                                        layer.closeAll('loading');
                                        if (res.code == 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            obj.del();
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    }, 'DELETE');
                                }else{
                                    obj.del();
                                }
                                productSelectMap.delete(obj.data.skuCode);
                                productSelectData=productSelectData.filter(item=>{
                                    if(item.skuCode==obj.data.skuCode){
                                        return false;
                                    }
                                    return true;
                                })
                            });
                        }
                    }

                    function showEditProductModel(data) {
                        var windownIndex=admin.open({
                            type: 1,
                            area: '1280px',
                            offset: '65px',
                            title: '添加配货需求物资信息',
                            content: $('#platformProductTab').html(),
                            success: function () {

                                var currentPageData=[];
                                // 渲染表格
                                var insTb = table.render({
                                    elem: '#platformProductTable',
                                    url: config.base + 'enterprisecab/productCab/admin/productDetail/list',
                                    page: true,
                                    cellMinWidth: 100,
                                    cols: [[
                                        {type: 'checkbox'}
                                        // ,{field: 'creatorName', sort: true, title: '创建人'}
                                        // ,{field: 'modifierName', sort: true, title: '修改人'}
                                        // ,{field: 'productId', sort: true, title: '商品ID'}
                                        ,{field: 'skuCode', sort: true, title: '物料编号'}
                                        ,{field: 'skuName', sort: true, title: '物料名称'}
                                        ,{field: 'skuSpecValueNames', sort: true, title: '商品规格码'}
                                        ,{field: 'unitName', sort: true, title: '单位'}
                                        ,{field: 'productBrandName', sort: true, title: '品牌'}
                                        ,{field: 'quantityStock', sort: true, title: '当前库存'}
                                        ,{field: 'marketPrice', sort: true, title: '单价'}
                                        ,{field: 'productType', sort: true, title: '是否刀具',templet:function (d) {
                                                if(d.productType=='knife'){
                                                    return '是';
                                                }
                                                return '否';
                                            }}

                                        // ,{field: 'skuSpec', sort: true, title: '商品规格列表'}
                                        // ,{field: 'skuSpecValue', sort: true, title: '商品规格值列表'}
                                        //
                                        // ,{field: 'enterpriceCode', sort: true, title: '企业货号'}
                                        // ,{field: 'spPrice', sort: true, title: '渠道价'}
                                        //
                                        // ,{field: 'storePrice', sort: true, title: '商品店铺价'}
                                        // ,{field: 'activityPrice', sort: true, title: '最低活动价'}
                                        // ,{field: 'supplyPrice', sort: true, title: '平台供货价'}
                                        // ,{field: 'stockNumber', sort: true, title: '库存数'}
                                        // ,{field: 'barcode', sort: true, title: '商品条码'}
                                        // ,{field: 'sortNumber', sort: true, title: '排序'}
                                        // ,{field: 'settledCompanyId', sort: true, title: '入驻企业ID'}
                                        // ,{field: 'isSyns', sort: true, title: '是否已同步：no(0,未同步);yes(1,已同步);'}
                                        // ,{field: 'dataSourceTypeDesc', sort: true, title: '数据来源'}
                                        // ,{field: 'idPlatform', sort: true, title: '平台端表id'}
                                        // ,{field: 'synsTime', sort: true, title: '同步时间'}
                                        // ,{field: 'skuCodeAs', sort: true, title: '用于区分云端云仓手动添加物料'}
                                        // ,{field: 'createTime', sort: true, title: '创建时间'}
                                        // ,{align: 'center', toolbar: '#productDetailTableBar', title: '操作', width: 200}
                                    ]],
                                    done: function (res, curr, count) {

                                        currentPageData=res.data;
                                        var isCheckAll=true;
                                        var isAllSelected=true;
                                        for(var k=0;k<res.data.length;k++){
                                            res.data[k].LAY_CHECKED = true;
                                            var index= res.data[k]['LAY_TABLE_INDEX'];
                                            var checkbox = $('#'+'platformProductTable'+'').next().find('tr[data-index=' + index + '] input[type="checkbox"]');
                                            if(checkedMap.get(res.data[k].id)){
                                                checkbox.prop('checked', true).next().addClass('layui-form-checked');
                                            }else{
                                                isCheckAll=false;
                                            }
                                            if(productSelectMap.get(res.data[k].skuCode)){
                                                $(checkbox[0]).parent().remove();
                                            }else{
                                                isAllSelected=false;
                                            }
                                        }
                                        if(isCheckAll){
                                            $(' [lay-filter="layTableAllChoose"]').prop('checked', true).next().addClass('layui-form-checked');
                                        }
                                        if(isAllSelected){
                                            $($('[lay-filter="layTableAllChoose"]')[0]).parent().hide();
                                        }else{
                                            $($('[lay-filter="layTableAllChoose"]')[0]).parent().show();
                                        }
                                        //移除按钮
                                        // var menuButton = config.getMenuButton(location.hash);
                                        // if(menuButton.indexOf("搜索") == -1){
                                        //     $('.layui-form.toolbar').remove();
                                        // }
                                        // if(menuButton.indexOf("修改") == -1){
                                        //     $("[lay-event='edit']").remove();
                                        // }
                                        // if(menuButton.indexOf("删除") == -1){
                                        //     $("[lay-event='del']").remove();
                                        // }
                                    }
                                });

                                // 工具条点击事件
                                table.on('tool(platformProductTable)', function (obj) {
                                    var data = obj.data;
                                    if (obj.event === 'edit') { //修改
                                        showEditModel(data);
                                    } else if (obj.event === 'del') { // 删除
                                        doDelete(obj);
                                    }
                                });

                                //监听排序
                                table.on('sort(platformProductTable)', function(obj){
                                    table.reload('platformProductTable', {
                                        initSort: obj
                                        ,where: {
                                            sort: obj.field
                                            ,order: obj.type
                                        }
                                    });
                                });

                                // 搜索
                                form.on('submit(formSubSearchLog4Product)', function (data) {
                                    insTb.reload({where: data.field,page: {curr: 1}},'data');
                                });


                                var checkedMap=new Map();
                                var checkedData=[];
                                table.on('checkbox(platformProductTable)', function(obj){
                                    var data = obj.data;
                                    if(obj.type=='all'){
                                        if(obj.checked){
                                            var checkStatus = table.checkStatus('platformProductTable')
                                            var checkedAllData = checkStatus.data;
                                            checkedAllData.forEach(item=> {

                                                    if (checkedMap.get(item.id) == undefined) {
                                                        checkedData.push(item);
                                                    }
                                                    checkedMap.set(item.id,item.id);


                                            })


                                        }else{
                                            currentPageData.forEach(item=>{
                                                if(checkedMap.get(item.id)!=undefined){
                                                    checkedData=checkedData.filter(item2=>{
                                                        if(item.id==item2.id){
                                                            return false;
                                                        }
                                                        return  true;
                                                    })
                                                }
                                                checkedMap.delete(item.id);

                                            })
                                        }
                                    }else{
                                        if(obj.checked){
                                            if (checkedMap.get(data.id) == undefined) {
                                                checkedData.push(data);
                                            }
                                            checkedMap.set(data.id,data.id);
                                        }else{
                                            if(checkedMap.get(data.id)!=undefined){
                                                checkedData=checkedData.filter(item=>{
                                                    if(item.id==data.id){
                                                        return false;
                                                    }
                                                    return  true;
                                                })
                                            }
                                            checkedMap.delete(data.id);
                                        }
                                        var isAllChecked=true;
                                        currentPageData.forEach(item=>{

                                                if(checkedMap.get(item.id)==undefined){
                                                    isAllChecked=false;
                                                }


                                        })
                                        if(isAllChecked){
                                            $('[lay-filter="layTableAllChoose"]').prop('checked', true).next().addClass('layui-form-checked');
                                        }else{
                                            $('[lay-filter="layTableAllChoose"]').prop('checked', false).next().removeClass('layui-form-checked');
                                        }
                                    }

                                });

                                $("#distributionDemandProductBtnAdd").click(function () {
                                    fillSelectProduct(checkedData)
                                    layer.close(windownIndex)
                                })

                            }
                        })
                    }
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/order/admin/distributionDemand/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#distributionDemandBtnExport').click(function () {
            var checkRows = table.checkStatus('distributionDemandTempTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>